/**
 * 自定义公共函数包
 * 这里可以写你自己的公共函数
 */
var pubFun = {};
/**
 * 公共函数写法示例
 * 调用示例：
 * pubFun.test();
 * 或
 * vk.myfn.test(); vk.myfn.uniqueArrayjs()
 */
pubFun.test = function(path) {
	// 此处可以直接使用 vk 实例
	let timeStr = vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss");
	// 逻辑
	// ...
	// 逻辑	
	return {
		timeStr,
		msg: "这是公共函数test的返回"
	}
}
pubFun.uniqueArrayjs = function(arr) {
	//去数组对象重复
	return arr.filter((item, index) => {
		return arr.findIndex(obj => obj.jsbh === item.jsbh) === index;
	});

}
pubFun.generateUUID = function() {
	//去数组对象重复
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0,
        v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });

}
pubFun.aaaa1 = function(arrss) {
	//1.排名
	arrss.sort((a, b) => b.scores - a.scores);
	for (let i = 0; i < arrss.length; i++) {
		// 如果当前分数与前一个分数相同，则排名不变
		arrss[i].pmcj = i + 1
	}
	let uniqueScores = new Set(arrss.map(student => student.scores));
	let scoresWithCount = Array.from(uniqueScores).map(scores => {
		return {
			scores,
			count: arrss.filter(student => student.scores === scores).length
		};
	});
	let arrcont = scoresWithCount.sort((a, b) => b.count - a.count);

	for (let i = 0; i < arrss.length; i++) {
		arrss[i].cjsr = arrss.length
		for (let x = 0; x < arrcont.length; x++) {
			if (arrss[i].scores === arrcont[x].scores) {
				arrss[i].count = arrcont[x].count
			}
		}
	}
	arrss.push({
		cjsr: 9999,
		count: 9999,
		pmcj: 9999,
		scores: 9999
	})
	//3.得出优率名次并赋值到数组中
	let tempaaca = 0;
	let jlyg = []
	let gys = 0;
	for (let q = 0; q < arrss.length; q++) {
		if (q > 0 && arrss[q].scores == arrss[q - 1].scores) {
			jlyg.push(arrss[q])
		} else {
			tempaaca = 0
			gys = 0
			if (q > 0 && jlyg.length >= 1) {
				for (let i = 0; i < jlyg.length; i++) {
					tempaaca += (jlyg[i].pmcj % arrss.length)
				}
				gys = tempaaca / jlyg.length
				arrss[q - 1].gys = gys
			}
			jlyg = []
			jlyg.push(arrss[q])
		}
	}
	arrss.pop();
	arrss.sort((a, b) => b.pmcj - a.pmcj);
	for (let u = 0; u < arrss.length; u++) {
		if (u > 0 && arrss[u].scores == arrss[u - 1].scores) {
			arrss[u].gys = arrss[u - 1].gys
		}
	}

	return arrss

}


module.exports = pubFun;